package com.med.util;

import com.med.entity.DepartmentDO;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: Ecard_SSM
 * @BelongsPackage: com.cykj.util
 * @Author: 小吴
 * @CreateTime: 2025-07-03 15:08
 * @Description: 构建树形菜单工具类
 * @Version: 1.0
 */
public class TreeMenuUtils {

    /**
     * @Description: 构建树形菜单
     * @Param:[allMenuList] : 根据角色查询出来的菜单数据
     * @return: java.util.List<com.cykj.entity.CyMenuDO>
     */
    public static List<DepartmentDO> buildTreeMenu(List<DepartmentDO> allMenuList){

        List<DepartmentDO> treeMenu = new ArrayList<>();

        //1.遍历菜单
        for (DepartmentDO item : allMenuList) {
            //2.寻找父级菜单
            if (item.getDepartmentParentId() == 0) {
                //3.寻找子菜单
                DepartmentDO menu = getChildrenMenu(item, allMenuList);

                treeMenu.add(menu);
            }
        }
        return treeMenu;
    }


    /**
     * @Description: 寻找子菜单
     * @Param:
     * @return:
     */
    private static DepartmentDO getChildrenMenu(DepartmentDO parentMenu, List<DepartmentDO> allMenuList) {

        //1.遍历菜单
        for (DepartmentDO item : allMenuList) {
            //判断是否为parentMenu 的子菜单
            if (parentMenu.getDepartmentId() == item.getDepartmentParentId()) {
                //子菜单 item

                //判断子菜单 有没有 子菜单
                DepartmentDO childrenMenu = getChildrenMenu(item, allMenuList);

                //父级菜单的子菜单
                parentMenu.getChildren().add(childrenMenu);
            }
        }
        return parentMenu;
    }


}
